package com.xiaomazi.edu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaomazi.common.utils.R;
import com.xiaomazi.edu.dto.TeacherQuery;
import com.xiaomazi.edu.entity.Course;
import com.xiaomazi.edu.service.CourseService;
import com.xiaomazi.servicebase.exception.LLXYException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomazi.edu.entity.Teacher;
import com.xiaomazi.edu.mapper.TeacherMapper;
import com.xiaomazi.edu.service.TeacherService;

import java.util.List;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService{

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private CourseService courseService;

    /**
     * 根据讲师Id查询讲师信息和主讲课程
     * @param teacherId 讲师Id
     * @return data
     */
    @Override
    public R getTeacherInfoById(String teacherId) {
        List<Course> courseList = courseService.query().eq("teacher_id", teacherId).list();
        Teacher teacher = this.getById(teacherId);
        return R.ok().data("courseList",courseList).data("teacherInfo",teacher);
    }

    /**
     * 根据多重条件查询讲师
     * @param teacherQuery
     * @return
     */
    @Override
    public R pageListTeacherByQuery(TeacherQuery teacherQuery) {
        String name = teacherQuery.getName();
        Integer current = teacherQuery.getCurrent();
        Integer size = teacherQuery.getSize();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        IPage<Teacher> page = new Page<>(current, size);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();

        if (StrUtil.isNotBlank(name)){
            queryWrapper.like("name",name);
        }
        if (null != level){
            queryWrapper.eq("level",level);
        }
        if (StrUtil.isNotBlank(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if (StrUtil.isNotBlank(end)){
            queryWrapper.le("gmt_create",end);
        }

        queryWrapper.orderByDesc("gmt_create");
        page = this.page(page,queryWrapper);

        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());
    }
}
